﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace LPV_POS.mh_pos.mh_cauhinh
{
    public partial class mh_dm_mayin_DS1 : DevExpress.XtraEditors.XtraForm
    {

        public mh_dm_mayin_DS1()
        {
            InitializeComponent();
        }

        private void mh_dm_mayin_bep_Load(object sender, EventArgs e)
        {
            this.showData_BEP_MayIn();
            this.showData_BEP_CauHinh();
            //
            this.showData_HoaDon_MayIn();
            this.showData_HoaDon_CauHinh();
            //
            this.showData_BIENNHAN_MayIn();
            this.showData_BIENNHAN_CauHinh();
        }

        #region "   BEP" 
        private void simpleButton5_Click(object sender, EventArgs e)
        {
            //them            
            this.ShowEdit_BEP("-1");
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            //xoa
            string ma = "";
            DataRow dr = gridView_Bep.GetDataRow(gridView_Bep.FocusedRowHandle);
            ma = dr["DM_MAYIN_BEP_MA"] + "";

            if (ma != "") 
            {
                if (new mh_thongbao("Bạn muốn xóa", 4).ShowForm() == DialogResult.OK)
                {
                    string sSQL = "DELETE FROM DM_MAYIN_BEP WHERE DM_MAYIN_BEP_MA=" + ma;
                    int irt = new thuvien().SP_Update("SP_EXECUTESQL",
                                                    new string[] { "@sSQL" }, new object[] { sSQL });

                    this.showData_BEP_MayIn();
                }
            }
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            //sua
            string ma = "";
            DataRow dr = gridView_Bep.GetDataRow(gridView_Bep.FocusedRowHandle);
            ma = dr["DM_MAYIN_BEP_MA"] + "";
            this.ShowEdit_BEP(ma);
        }

        void showData_BEP_MayIn()
        { 
            thuvien othuvien = new  thuvien();
            string sSQL = "SELECT * FROM DM_MAYIN_BEP";

            DataTable tbInBep = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            gridControl_Bep.DataSource = tbInBep.Copy();

            

        }
        void showData_BEP_CauHinh()
        {
            DataTable tbCauHinh;
            thuvien othuvien = new thuvien();
            string sSQL = "SELECT * FROM DM_MAYIN_BEP_CAUHINH";
            tbCauHinh = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            
            if (tbCauHinh.Rows.Count > 0)
            {
                Boolean bchkBEP_IN_GIA = true;
                Boolean bchkBEP_IN_SO_HOADON = true;
                Boolean bchkBEP_IN_TEN_BAN = true;
                Boolean bchkBEP_IN_TEN_HANGHOA = true;

                Boolean.TryParse(tbCauHinh.Rows[0]["IN_GIA"] + "", out bchkBEP_IN_GIA);
                Boolean.TryParse(tbCauHinh.Rows[0]["IN_SO_HOADON"] + "", out bchkBEP_IN_SO_HOADON);
                Boolean.TryParse(tbCauHinh.Rows[0]["IN_TEN_BAN"] + "", out bchkBEP_IN_TEN_BAN);
                Boolean.TryParse(tbCauHinh.Rows[0]["IN_TEN_HANGHOA"] + "", out bchkBEP_IN_TEN_HANGHOA);

                chkBEP_IN_GIA.Checked = bchkBEP_IN_GIA;
                chkBEP_IN_SO_HOADON.Checked = bchkBEP_IN_SO_HOADON;
                chkBEP_IN_TEN_BAN.Checked = bchkBEP_IN_TEN_BAN;
                chkBEP_IN_TEN_HANGHOA.Checked = bchkBEP_IN_TEN_HANGHOA;
            }
            else
            {
                chkBEP_IN_GIA.Checked = true;
                chkBEP_IN_SO_HOADON.Checked = true;
                chkBEP_IN_TEN_BAN.Checked = true;
                chkBEP_IN_TEN_HANGHOA.Checked = true;
            }
        }
        void ShowEdit_BEP(string _ma)
        {
            mh_dm_mayin_bep_info omh_dm_mayin_bep_info = new mh_dm_mayin_bep_info(mh_dm_mayin_bep_info.bang.DM_MAYIN_Bep, _ma); 
            omh_dm_mayin_bep_info.ShowDialog();
            if (omh_dm_mayin_bep_info.DialogResult == DialogResult.OK)
                this.showData_BEP_MayIn();
        }

        private void btnLuu_BEP_Click(object sender, EventArgs e)
        {
            string sSQL = "UPDATE DM_MAYIN_BEP_CAUHINH "+
                          "SET "+
                          "     IN_GIA = '" + chkBEP_IN_GIA.Checked + "' "+
                          ",    IN_TEN_HANGHOA = '" + chkBEP_IN_TEN_HANGHOA.Checked + "' " +
                          ",    IN_TEN_BAN = '" + chkBEP_IN_TEN_BAN.Checked + "' " +
                          ",    IN_SO_HOADON = '" + chkBEP_IN_SO_HOADON.Checked+ "' "
                          ;

            int irt = new thuvien().SP_Update("SP_EXECUTESQL",
                                            new string[] { "@sSQL" }, new object[] { sSQL });
            if (irt >= 0)
                new mh_thongbao("Lưu thành công", 2).ShowDialog();
        }

        private void btnThoat_Bep_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        #endregion

        #region "   Hoa Don"


        private void simpleButton2_Click(object sender, EventArgs e)
        {
            //them
            this.ShowEdit_HoaDon("-1");
        }

        private void simpleButton8_Click(object sender, EventArgs e)
        {
            //xoa
            string ma = "";
            DataRow dr = gridView_HoaDon.GetDataRow(gridView_HoaDon.FocusedRowHandle);
            ma = dr["DM_MAYIN_HD_MA"] + "";

            if (ma != "")
            {
                if (new mh_thongbao("Bạn muốn xóa", 4).ShowForm() == DialogResult.OK)
                {
                    string sSQL = "DELETE FROM DM_MAYIN_HD WHERE DM_MAYIN_HD_MA=" + ma;
                    int irt = new thuvien().SP_Update("SP_EXECUTESQL",
                                                    new string[] { "@sSQL" }, new object[] { sSQL });

                    this.showData_HoaDon_MayIn();
                }
            }
        }

        private void simpleButton7_Click(object sender, EventArgs e)
        {
            //sua
            string ma = "";
            DataRow dr = gridView_HoaDon.GetDataRow(gridView_HoaDon.FocusedRowHandle);
            ma = dr["DM_MAYIN_HD_MA"] + "";
            this.ShowEdit_HoaDon(ma);
        }

        private void btnThoat_HoaDon_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        void showData_HoaDon_MayIn()
        {
            thuvien othuvien = new thuvien();
            string sSQL = "SELECT * FROM DM_MAYIN_HD";

            DataTable tbInHoaDon = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            gridControl_HoaDon.DataSource = tbInHoaDon.Copy();
        }
        void showData_HoaDon_CauHinh()
        {
            DataTable tbDM_MAYIN_HD_CAUHINH;
            thuvien othuvien = new thuvien();
            string sSQL = "SELECT * FROM DM_MAYIN_HD_CAUHINH";
            tbDM_MAYIN_HD_CAUHINH = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });

            if (tbDM_MAYIN_HD_CAUHINH.Rows.Count > 0)
            {
               
            }
            else
            {
              
            }
        }
        void ShowEdit_HoaDon(string _ma)
        {
            mh_dm_mayin_bep_info omh_dm_mayin_bep_info = new mh_dm_mayin_bep_info(mh_dm_mayin_bep_info.bang.DM_MAYIN_HoaDon, _ma);
            omh_dm_mayin_bep_info.ShowDialog();
            if (omh_dm_mayin_bep_info.DialogResult == DialogResult.OK)
                this.showData_HoaDon_MayIn();
        }

        private void btnLuu_HoaDon_Click(object sender, EventArgs e)
        {
            string sSQL = "";

            int irt = new thuvien().SP_Update("SP_EXECUTESQL",
                                            new string[] { "@sSQL" }, new object[] { sSQL });
            if (irt >= 0)
                new mh_thongbao("Lưu thành công", 2).ShowDialog();
        }
        #endregion

        #region "   Bien Nhan"


        private void btnBN_Them_Click(object sender, EventArgs e)
        {
            //them
            this.ShowEdit_BIENNHAN("-1");
        }

        private void btnBN_Xoa_Click(object sender, EventArgs e)
        {
            //xoa
            string ma = "";
            DataRow dr = gridView_biennhan.GetDataRow(gridView_biennhan.FocusedRowHandle);
            ma = dr["DM_MAYIN_DM_MAYIN_BIENNHAN_MA"] + "";

            if (ma != "")
            {
                if (new mh_thongbao("Bạn muốn xóa", 4).ShowForm() == DialogResult.OK)
                {
                    string sSQL = "DELETE FROM DM_MAYIN_BIENNHAN WHERE DM_MAYIN_BIENNHAN_MA=" + ma;
                    int irt = new thuvien().SP_Update("SP_EXECUTESQL",
                                                    new string[] { "@sSQL" }, new object[] { sSQL });

                    this.showData_BIENNHAN_MayIn();
                }
            }
        }

        private void btnBN_Sua_Click(object sender, EventArgs e)
        {
            //sua
            string ma = "";
            DataRow dr = gridView_biennhan.GetDataRow(gridView_biennhan.FocusedRowHandle);
            ma = dr["DM_MAYIN_BIENNHAN_MA"] + "";
            this.ShowEdit_BIENNHAN(ma);
        }

        private void btnThoat_BIENNHAN_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        void showData_BIENNHAN_MayIn()
        {
            thuvien othuvien = new thuvien();
            string sSQL = "SELECT * FROM DM_MAYIN_BIENNHAN";

            DataTable tbInBN = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
            gridControl_BienNhan.DataSource = tbInBN.Copy();
        }
        void showData_BIENNHAN_CauHinh()
        {
            DataTable tbDM_MAYIN_BIENNHAN_CAUHINH;
            thuvien othuvien = new thuvien();
            string sSQL = "SELECT * FROM DM_MAYIN_BIENNHAN_CAUHINH";
            tbDM_MAYIN_BIENNHAN_CAUHINH = othuvien.SP_SelectDataTable("SP_EXECUTESQL", new string[] { "@sSQL" }, new object[] { sSQL });
          
        }
        void ShowEdit_BIENNHAN(string _ma)
        {
            mh_dm_mayin_bep_info omh_dm_mayin_bep_info = new mh_dm_mayin_bep_info(mh_dm_mayin_bep_info.bang.DM_MAYIN_BienNhan, _ma);
            omh_dm_mayin_bep_info.ShowDialog();
            if (omh_dm_mayin_bep_info.DialogResult == DialogResult.OK)
                this.showData_BIENNHAN_MayIn();
        }

        private void btnLuu_BN_Click(object sender, EventArgs e)
        {
            string sSQL = ""
                          ;

            int irt = new thuvien().SP_Update("SP_EXECUTESQL",
                                            new string[] { "@sSQL" }, new object[] { sSQL });
            if (irt >= 0)
                new mh_thongbao("Lưu thành công", 2).ShowDialog();
        }
        #endregion

        private void btnLuuNhommayIn_Click(object sender, EventArgs e)
        {
            string sDM_MAYIN_BEP_MA = "";

            DataRow dr1 = gridView_Bep.GetDataRow(gridView_Bep.FocusedRowHandle);
            if (dr1 != null)
            {
                sDM_MAYIN_BEP_MA = dr1["DM_MAYIN_BEP_MA"] + "";
            }
            thuvien othuvien = new thuvien();
            foreach (DataRow dr in tbInBep_In.Rows)
            {
                DataRowState sState = dr.RowState;

                string sMa = sDM_MAYIN_BEP_MA;

                string och = dr["chon"] + "";
                if (och + "" == "False")
                {
                    och = och + "";
                    sMa = "0";
                }
                ///
                if (sState == DataRowState.Modified)
                {
                    string sSQL = "UPDATE DM_NHOMCHUHH SET DM_MAYIN_BEP_MA = " + sMa + " WHERE OBJID LIKE '" + dr["OBJID"] + "' ";
                    othuvien.SP_Update("SP_EXECUTESQL", new string[] { "@sSQL"}, new object[] { sSQL});
                }
            }
        }
        DataTable tbInBep_In = null;
        private void gridView_Bep_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            string sDM_MAYIN_BEP_MA = "";

            DataRow dr = gridView_Bep.GetDataRow(gridView_Bep.FocusedRowHandle);
            if (dr != null)
            {
                sDM_MAYIN_BEP_MA = dr["DM_MAYIN_BEP_MA"] + "";
                tbInBep_In = new thuvien().SP_SelectDataTable("sp_DM_MAYIN_BEP",
                                                                    new string[] { "@flag", "@DM_MAYIN_BEP_MA" },
                                                                    new object[] { 5, sDM_MAYIN_BEP_MA });
                gridControl_Bep_In.DataSource = tbInBep_In;
            }
        }

      
        
    }
}
